package core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import javax.swing.JFrame;

/**
 *
 * @author Marcin
 */

public class Session {
    private static JFrame parent;
    private boolean isLogged;
    private Status status;
    private User user;
    public Session()
    {
        parent = null;
        isLogged = false;
    }
    public Session(JFrame parent)
    {
        this.parent = parent;
        isLogged = false;
    }
    public boolean logIn(String username,String password)
    {
        try {
            
            Statement stmt = SqlConnection.getInstance().getSqlConnection().createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);           
            
            String query= "SELECT id,id_uprawnienia,password,imie,nazwisko,email,is_active FROM users WHERE login= '"+username+"'";
            stmt.execute(query);
            ResultSet rs = stmt.getResultSet();
            
            if(rs.next() &&(rs.getString(3) != null) && rs.getString(3).equals(password) && rs.getBoolean(7)) {
                isLogged = true;
                status =  Status.values()[rs.getInt(2)-1];
                user = new User(rs.getInt(1),Status.values()[rs.getInt(2)-1],username,rs.getString(4),rs.getString(5),rs.getString(6),rs.getBoolean(7),parent);
                return true;
            }
            else {
                ErrorLogger.getInstance().log("Niepoprawny login lub hasło", true, parent);
                return false;
            }
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
        }
        return false;
    }
    public boolean isLogged()
    {
        return isLogged;
    }
    public Status getStatus()
    {
        return status;
    }
    public User getUser()
    {
        return user;
    }
    
    public static LinkedList<User> getAllUsers(){
        LinkedList<User> users = new LinkedList<>();
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT id, id_uprawnienia,login,password,imie,nazwisko,email,is_active FROM users");
            ResultSet rs = stmn.getResultSet();
            while(rs.next()){
                Status statusTemp = Status.values()[rs.getInt(2)-1];
                users.addLast(new User(rs.getInt(1),statusTemp,rs.getString(3),rs.getString(5),rs.getString(6),rs.getString(7),rs.getBoolean(8),parent));    
            }
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
        }
        return users;
    }   
    
    public static LinkedList<User> getUsers(int id, Status status,String login, String FirstName, String LastName, String eMail, String isActive){
        LinkedList<User> users = new LinkedList<>();
        
        if(status==null && login==null && FirstName==null && LastName==null && eMail==null && isActive==null){
            ErrorLogger.getInstance().log("Nie wprowadzono danych", true, parent);
            return users;
        }//sprawdzenie poprawnosci
        
        String query="SELECT id,id_uprawnienia,login,password,imie,nazwisko,email,is_active FROM users WHERE ";
        boolean isfirstWhere=true;//flaga do AND'ów
        
        if(status!=null){
            query+="id_uprawnienia="+status.ordinal();
            isfirstWhere=false;
        }
        if(isfirstWhere==false && id!=-1) query+=" AND ";
        
        if(id!=-1){
            query+="id="+id;
            isfirstWhere=false;
        }       
        
        if(isfirstWhere==false && login!=null) query+=" AND ";
        
        if(login!=null){
            query+="login="+login;
            isfirstWhere=false;
        }
       
        if(isfirstWhere==false && FirstName!=null) query+=" AND ";
        
        if(FirstName!=null){
            query+="imie='"+FirstName+"'";
           isfirstWhere=false;
        }
        
        if(isfirstWhere==false && LastName!=null) query+=" AND ";
       
        if(LastName!=null){
            query+="nazwisko='"+LastName+"'";
            isfirstWhere=false;
        }
        if(isfirstWhere==false && eMail!=null) query+=" AND ";
        
        if(eMail!=null){
            query+="email='"+eMail+"'";
            isfirstWhere=false;
        }
        if(isfirstWhere==false && isActive!=null) query+=" AND ";
        
        if(isActive!=null) query+="is_active="+isActive+"";
        query+=";";
        //koniec zapytania        
        
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            
            while(rs.next()){
                Status statusTemp = Status.values()[rs.getInt(2)-1];
                users.addLast(new User(rs.getInt(1),statusTemp,rs.getString(3),rs.getString(5),rs.getString(6),rs.getString(7),rs.getBoolean(8),parent));    
             }
            
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
            return users;
        }              
        
        return users;
        
    }
    
    public static void BackUp(String path){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="BACKUP DATABASE [BazaPrzychodni] TO  DISK = N'"+path+"' WITH NOFORMAT, NOINIT,  NAME = N'BazaPrzychodni-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";   
            
            stmn.execute(query);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log(ex.getMessage(), true,parent);
        }
    }
}
